package com.twang.stock.service.jinyan;

import com.twang.stock.entity.*;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.mapper.HotTagMapper;
import com.twang.stock.mapper.StockTagMapper;
import com.twang.stock.mapper.UserBusinessTypeMapper;
import com.twang.stock.enmu.HotTagEnum;
import com.twang.stock.enmu.UserBusinessTypeEnum;
import com.twang.stock.service.UserService;
import com.twang.stock.task.old.QinXuGaoDuTask;
import com.twang.stock.utils.date.HolidayCopy;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class UserBusinessTypeService {

    @Resource
    private UserBusinessTypeMapper userBusinessTypeMapper;
    @Autowired
    private UserService userService;
    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;
    @Autowired
    private HolidayService  holidayService;
    @Resource
    private HotTagMapper hotTagMapper;

    @Resource
    private StockTagMapper stockTagMapper;
    @Resource
    private QinXuGaoDuTask qinXuGaoDuTask;

    public UserBusinessType getUserBusinessType(Long userId,String type){
        UserBusinessType userBusinessTypeParam = new UserBusinessType();
        userBusinessTypeParam.setUserId(userId);
        userBusinessTypeParam.setType(UserBusinessTypeEnum.TYPE_HOT_TAG.getCode());
        userBusinessTypeParam.setDate(holidayService.getDayByNum(0));
        UserBusinessType userBusinessType = userBusinessTypeMapper.selectOne(userBusinessTypeParam);
        if (null == userBusinessType) {
            userBusinessTypeParam.setDate(holidayService.getDayByNum(1));
            userBusinessType = userBusinessTypeMapper.selectOne(userBusinessTypeParam);
            if (null == userBusinessType) {
                HotTag hotTag = new HotTag();
                hotTag.setHotTag(HotTagEnum.CORE_DUAN_XIAN_BAO.getCode() + "_" + holidayService.getDayByNum(0));
                hotTag.setUserId(userId);
                if(CollectionUtils.isEmpty(hotTagMapper.select(hotTag))){
                    hotTag.setCreateTime(new Date());
                    hotTag.setUpdateTime(new Date());
                    hotTag.setHotTemperatureName(HolidayCopy.getToday());
                    hotTag.setRemark("短线宝");
                    hotTagMapper.insert(hotTag);
                }

                HotTag param = new HotTag();
                param.setUserId(userId);
                param.setHotTag(hotTag.getHotTag());
                HotTag hotTag1 = hotTagMapper.selectOne(param);
                userBusinessTypeParam.setBusinessId(hotTag1.getId());
                userBusinessTypeMapper.insertSelective(userBusinessTypeParam);
                qinXuGaoDuTask.initChangXianChi();
                return userBusinessTypeMapper.selectOne(userBusinessTypeParam);
            }
        }
        return userBusinessType;
    }

    public void insertUserBusinessType(Long businessId, String type,String date){
        User user = userService.getUserByToken();
        UserBusinessType userBusinessType =getUserBusinessType(user.getId(), UserBusinessTypeEnum.TYPE_HOT_TAG.getCode());
        HotTag record = hotTagMapper.selectByPrimaryKey(userBusinessType.getBusinessId());
        //查询标签关联的权重个个股
        StockTag stockTag = new StockTag();
        stockTag.setTagId(record.getId());
        stockTag.setStockId(businessId.intValue());
        StockTag stockTagRecord = stockTagMapper.selectOne(stockTag);
        if (null == stockTagRecord) {
            stockTag.setCreateTime(new Date());
            stockTag.setUpdateTime(new Date());
            stockTagMapper.insert(stockTag);
        }
        // stockTagMapper.insert(stockTag);
        //
        insertUserBusinessTypeStockType(businessId,user.getId());
    }

    /**
     * 生成个人关注记录
     * @param businessId
     * @param userId
     */
    public void insertUserBusinessTypeStockType(Long businessId,Long userId){
        //个人
        UserBusinessType userBusinessType=new UserBusinessType();
        userBusinessType.setType(UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode());
        userBusinessType.setBusinessId(businessId);
        userBusinessType.setUserId(userId);
        if(CollectionUtils.isEmpty(userBusinessTypeMapper.select(userBusinessType))){
            userBusinessType.setDate(HolidayCopy.getRealToday());
            userBusinessTypeMapper.insert(userBusinessType);
        }
    }

    public void deletedUserBusinessType(Long businessId, String type,String date) {
        User user = userService.getUserByToken();
        UserBusinessType userBusinessType = getUserBusinessType(user.getId(), UserBusinessTypeEnum.TYPE_HOT_TAG.getCode());
        HotTag record = hotTagMapper.selectByPrimaryKey(userBusinessType.getBusinessId());
        //查询标签关联的权重个个股
        StockTag stockTag = new StockTag();
        stockTag.setTagId(record.getId());
        stockTag.setStockId(businessId.intValue());
        stockTagMapper.delete(stockTag);
        //
        deleteUserBusinessTypeStockType(businessId,user.getId());
    }

    /**
     * 删除个人关注记录
     * @param businessId
     * @param userId
     */
    public void deleteUserBusinessTypeStockType(Long businessId,Long userId) {
        //个人
        UserBusinessType userBusinessType = new UserBusinessType();
        userBusinessType.setType(UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode());
        userBusinessType.setBusinessId(businessId);
        userBusinessType.setUserId(userId);
        userBusinessTypeMapper.delete(userBusinessType);
    }

    public List<Long> selectStockIds(Long id,String type) {
        User user = userService.getUserByToken();
//        UserBusinessType userBusinessType = getUserBusinessType(user.getId(), UserBusinessTypeEnum.TYPE_HOT_TAG.getCode());
//        if (null == userBusinessType) {
//            return null;
//        }
        //查询标签关联的权重个个股
        UserBusinessType param=new UserBusinessType();
        param.setType(UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode());
        param.setUserId(user.getId());
        List<UserBusinessType> userBusinessTypeList = userBusinessTypeMapper.select(param);
        if (CollectionUtils.isNotEmpty(userBusinessTypeList)) {
            return userBusinessTypeList.stream().map(UserBusinessType::getBusinessId).collect(Collectors.toList());
        }
        return null;
    }

    /**
     *
     * @param userId
     * @param type
     * @return
     */
    public List<String> selectStockCodeInfo(Long userId,String type) {
        UserBusinessType userBusinessType = getUserBusinessType(userId, type);
        if (null == userBusinessType) {
            return null;
        }
        HotTag record = hotTagMapper.selectByPrimaryKey(userBusinessType.getBusinessId());
        //查询标签关联的权重个个股
        UserBusinessType param=new UserBusinessType();
        param.setType(UserBusinessTypeEnum.TYPE_STOCK_INFO.getCode());
        param.setUserId(userId);
        List<UserBusinessType> userBusinessTypeList = userBusinessTypeMapper.select(param);
        if (CollectionUtils.isNotEmpty(userBusinessTypeList)) {
           List<Long>ids= userBusinessTypeList.stream().map(UserBusinessType::getBusinessId).collect(Collectors.toList());
            Example example = new Example(CompanyCodeInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id",ids);
            List<CompanyCodeInfo> companyCodeInfoList = companyCodeInfoMapper.selectByExample(example);
            return companyCodeInfoList.stream().map(item -> {
                return item.getCompanyCode();
            }).collect(Collectors.toList());
        }
        return null;
    }
}
